﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//using IrisGenerator.Common.Core.Rules;
using System.Reflection;
using IrisGenerator.Framework.Rules.Reflection;

namespace IrisGenerator.Framework.Reflection
{
    public class MethodCall
    {
        
        private MethodCallParameterCollection _parameters;
        private MethodCallParameterCollection _orderedParameters;

        public MethodCallParameterCollection Parameters
        {
            get
            {
                if (_parameters == null)
                    _parameters = new MethodCallParameterCollection();
                return _parameters;
            }
        }


        private string _typeName;

        public Type ParentType { get; set; }

        public string TypeName
        {
            get
            {
                return _typeName;
            }
            set
            {
                _typeName = value;
                if(!string.IsNullOrEmpty(value))
                    this.ParentType = Assembly.GetCallingAssembly().GetType(value);
            }
        }

        //public string Type { get; set; }
        public string MethodName { get; set; }
        public object Invoke(object TargetObject,out bool result)
        {
            if (MethodCallRule.CanBeCalled(this))
            {
                try
                {
                    //System.Type type = this.ParentType;
                    MethodInfo method = this.GetMethod();

                    object returnValue = method.Invoke(TargetObject, SetParametersValues(TargetObject));
                    result = true;
                    return returnValue;
                }
                catch
                {
                    throw;
                }
                
            }
            else
            {
                result = false;
            }

            return null;
        }
        //public Type GetMethodType()
        //{
        //    //if (string.IsNullOrEmpty(this.Type))
        //    //    return null;

        //    //return System.Type.GetType(this.Type);
        //    return this.ParentType;
        //}
        public object[] GetParametersValues()
        {
            if (_orderedParameters == null)
                return null;

            object[] returnValue = new object[_orderedParameters.Count];
            for(int i=0;i<_orderedParameters.Count;i++)
            {
                returnValue[i] = _orderedParameters[i].PropertyName;
            }

            return returnValue;
        }
        public MethodInfo GetMethod()
        {
            if (string.IsNullOrEmpty(this.MethodName))
                return null;

            MethodInfo[] typeMethods=this.ParentType.GetMethods();
            foreach (MethodInfo method in typeMethods)
            {
                if (method.Name.Equals(this.MethodName))
                {
                    ParameterInfo[] methodParameters = method.GetParameters();
                    if (methodParameters.Length == 0 && this.Parameters.Count == 0)
                        return method;

                    if (methodParameters.Length.Equals(this.Parameters.Count))
                    {
                        _orderedParameters = new MethodCallParameterCollection();
                        foreach (ParameterInfo pi in methodParameters)
                        {
                            foreach (MethodCallParameter param in this.Parameters)
                            {
                                if (pi.Name.Equals(param.ParameterName))
                                    _orderedParameters.Add(param);
                            }
                        }
                        if (methodParameters.Length.Equals(_orderedParameters.Count))
                            return method;
                    }
                }
            }
            return null;
        }
        public object[] SetParametersValues(object targetObject)
        {
            object[] returnValue = new object[this.Parameters.Count];
            
            for (int i=0;i<this.Parameters.Count;i++)
            {
                if (this.Parameters[i].PropertyName != null)
                {
                    this.Parameters[i].BindValue(targetObject);
                    
                }
                returnValue[i] = this.Parameters[i].Value;
            }
            return returnValue;
        }

    }
}
